﻿#pragma once

#include "World/Chunk.h"
#include "Util/Hash.h"
#include "Core/Camera.h"

// std
#include <unordered_map>
#include <string>

class AppContext;

class BlockRegistry;

class Persistence
{
public:
  std::unordered_map<glm::ivec2, Ref<Chunk>, Util::HashIVec2> chunks{};

  Persistence(const std::string &path);
  ~Persistence();

  void CommitSeed(uint32_t seed);
  uint32_t GetSeed() const { return m_seed; };

  void CommitTime(uint32_t time) { m_time = time; };
  uint32_t GetTime() const { return m_time; };

  void CommitBlockRegistry(const Ref<BlockRegistry> &blockReg);
  Ref<BlockRegistry> GetBlockRegistry();

  void CommitCamera(const Ref<Camera> &camera);
  Ref<Camera> GetCamera();

  void CommitChunk(const Ref<Chunk> &chunk);
  Ref<Chunk> GetChunk(const glm::ivec2 &worldPosition);

  bool Save();
  bool Load();

private:
  std::string m_path;
  uint32_t m_seed = 0;
  float m_time = 0.0f;
  Ref<Camera> m_camera{nullptr};
  Ref<BlockRegistry> m_blockReg{nullptr};
};